from closure_linter.common import errorhandler

def GetJsonObjectForError(error):
  token = error.token
  token_json = {
    'type': token.type,
    'line': token.line,
    'line_number': token.line_number,
    'string': token.string,
    'type': token.type,
    'values': token.values
    }

  return {
    'code': error.code,
    'message': error.message,
    'start_index': error.start_index,
    'token': token_json
    }


class JsonErrorHandler(errorhandler.ErrorHandler):
  def __init__(self):
    self._errors = []

  def HandleError(self, error):
    self._errors.append(error)

  def GetJson(self):
    output_errors = map(GetJsonObjectForError, self._errors)

    output_json = {
      'errors': output_errors
      }

    return output_json
